use std::collections::*;
use std::io::*;
use std::string::*;

fn main() {
    let stdin = stdin();
    let mut lines = stdin.lock().lines().map(|line| line.unwrap());

    let n: i128 = lines.next().unwrap().trim().parse().unwrap();
    let v: Vec<i128> = lines
        .next()
        .unwrap()
        .chars()
        .map(|s| s.to_string().parse().unwrap())
        .collect();
    let sum: i128 = v.iter().sum();

    let (mut pre_sum, mut res) = (0, 0);
    let mut cnt: HashMap<i128, i128> = HashMap::new();

    for i in 0..n {
        *cnt.entry(pre_sum % 3).or_insert(0) += 1;
        pre_sum += v[i as usize];
        // 修复：传递引用给 get 方法
        res += *cnt.get(&((pre_sum - (sum % 3)) % 3)).unwrap_or(&0);
    }

    if sum % 3 == 0 {
        res += 1;
    }

    println!("{}", res);
}
